#region License
// MainNavigationPane.xaml.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Shammateh.Win.UI.Controls
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using Stema.Controls;

    /// <summary>
    /// Interaction logic for MainNavigationPane.xaml
    /// </summary>
    public partial class MainNavigationPane
    {
        // Using a DependencyProperty as the backing store for NavigationPaneHeaders.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NavigationPaneHeadersProperty =
            DependencyProperty.Register(
                "NavigationPaneHeaders",
                typeof(List<NavigationPaneHeader>),
                typeof(MainNavigationPane),
                new UIPropertyMetadata(OnNavigationPaneHeadersChange));

        /// <summary>
        /// Initializes a new instance of the <see cref="MainNavigationPane"/> class.
        /// </summary>
        public MainNavigationPane()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the navigation pane headers.
        /// </summary>
        /// <value>The navigation pane headers.</value>
        public List<NavigationPaneHeader> NavigationPaneHeaders
        {
            get { return (List<NavigationPaneHeader>)GetValue(NavigationPaneHeadersProperty); }
            set { SetValue(NavigationPaneHeadersProperty, value); }
        }

        /// <summary>
        /// Called when navigation pane headers change.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnNavigationPaneHeadersChange(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var mainNavigationPane = (MainNavigationPane)sender;
            mainNavigationPane.Fill();
        }

        /// <summary>
        /// Fills the navigation pane.
        /// </summary>
        private void Fill()
        {
            if (this.NavigationPaneHeaders == null)
            {
                return;
            }

            foreach (NavigationPaneHeader header in this.GetAuthorizedNavigationPaneHeaders())
            {
                var listBox = new ListBox { Style = FindResource("navigationPaneListBox") as Style };

                if (header.NavigationPaneDetails != null)
                {
                    foreach (INavigationPaneDetail detail in this.GetAuthorizedNavigationPaneDetails(header))
                    {
                        var image = new Image { Source = detail.Image, Height = 20 };
                        var run = new Run { Text = detail.Title };
                        var hyperLink = new Hyperlink(run)
                                        {
                                            Command = detail.Command,
                                            CommandParameter = detail.CommandParameter,
                                            Style = FindResource("navigationPaneHyperLink") as Style
                                        };
                        var textBlock = new TextBlock(hyperLink) { Margin = new Thickness(5) };
                        var stackPanel = new StackPanel
                                         { Orientation = Orientation.Horizontal, Children = { image, textBlock } };
                        var listBoxItem = new ListBoxItem
                                          {
                                              Content = stackPanel,
                                              Style = FindResource("navigationPaneListBoxItem") as Style
                                          };
                        listBox.Items.Add(listBoxItem);
                    }
                }

                var navigationPaneItem = new NavigationPaneItem
                                         {
                                             Header = header.Header,
                                             Image = header.Image,
                                             Content = listBox
                                         };

                this.navigationPane.Items.Add(navigationPaneItem);
            }
        }

        /// <summary>
        /// Gets the authorized navigation pane headers.
        /// </summary>
        /// <returns></returns>
        private IEnumerable<NavigationPaneHeader> GetAuthorizedNavigationPaneHeaders()
        {
            return this.NavigationPaneHeaders.Where(header => this.GetAuthorizedNavigationPaneDetails(header).Any());
        }

        /// <summary>
        /// Get the authorized navigation pane details.
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
        private IEnumerable<INavigationPaneDetail> GetAuthorizedNavigationPaneDetails(NavigationPaneHeader header)
        {
            return header.NavigationPaneDetails;
        }
    }
}
